package com.tongzhan.controller;

import com.tongzhan.base.Return;
import com.tongzhan.entity.religion.Condition;
import com.tongzhan.entity.religion.Site;
import com.tongzhan.entity.religion.Street;
import com.tongzhan.service.ConditionService;
import com.tongzhan.service.SiteService;
import com.tongzhan.service.StreetService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/condition")
public class ConditionController {

    @Resource
    private ConditionService conditionService;

    @Resource
    private StreetService streetService;

    @Resource
    private SiteService siteService;

    /**
     * 新增情况报告接口
     *
     * @param userId 用户id
     * @param streetCode 街道编码
     * @param siteCode 场所编码
     * @param title 主题
     * @param content 内容
     * @param fileUrls 图片路径
     * @return Return
     */
    @PostMapping("/newCondition")
    public Return newCondition(String userId, String streetCode, String siteCode,
                               String title, String content, String fileUrls) {
        if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(streetCode) || StringUtils.isEmpty(siteCode)
                || StringUtils.isEmpty(title) || "".equals(title.trim())) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (title.trim().length() > 1000) {
            return new Return(Return.FAIL, "标题过长");
        }
        if (StringUtils.isNotEmpty(content) && content.trim().length() > 5000) {
            return new Return(Return.FAIL, "内容过长");
        }
        Condition condition = new Condition();
        if (StringUtils.isNotEmpty(content)) {
            condition.setContent(content);
        }
        //街道
        Street street = streetService.selectByCode(streetCode);
        condition.setStreetCode(street.getStreetCode());
        condition.setStreetName(street.getStreetName());
        //场所
        Site site = siteService.selectByCode(siteCode);
        condition.setSiteCode(site.getSiteCode());
        condition.setSiteName(site.getSiteName());
        condition.setTitle(title.trim());
        condition.setPicture(fileUrls);
        return conditionService.newCondition(userId, condition);
    }

    /**
     * 时间段筛选情况列表接口
     *
     * @param userId 用户id
     * @param timeSlot 时间段
     * @param page 页
     * @param size 大小
     * @return Return
     */
    @PostMapping("/getConditionList")
    public Return getConditionList(String userId, String timeSlot, Integer page,
                                   @RequestParam(required = false, defaultValue = "10") Integer size) {
        if (page == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (page <= 0) {
            page = 1;
        }
        return conditionService.getConditionList(userId, timeSlot, page, size);
    }

    /**
     * 情况详情接口
     *
     * @param conditionCode 情况编码
     * @return Return
     */
    @PostMapping("/conditionDetails")
    public Return conditionDetails(String conditionCode) {
        if (conditionCode == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return conditionService.conditionDetails(conditionCode);
    }

    /**
     * 情况报告修改
     *
     * @param conditionCode 情况报告编码
     * @param streetCode 街道编码
     * @param siteCode 场所编码
     * @param title 主题
     * @param content 内容
     * @param fileUrls 图片路径
     * @return Return
     */
    @PostMapping("/update")
    public Return update(String conditionCode, String streetCode, String siteCode, String title,
                         String content, String fileUrls) {
        if (StringUtils.isEmpty(conditionCode) || StringUtils.isEmpty(streetCode) || StringUtils.isEmpty(siteCode)
                || StringUtils.isEmpty(title) || "".equals(title.trim())) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (title.trim().length() > 1000) {
            return new Return(Return.FAIL, "标题过长");
        }
        if (StringUtils.isNotEmpty(content) && content.trim().length() > 5000) {
            return new Return(Return.FAIL, "内容过长");
        }
        return conditionService.update(conditionCode, streetCode, siteCode, title, content, fileUrls);
    }

    /**
     * 情况报告删除
     *
     * @param conditionCode 情况报告编码
     * @return Return
     */
    @PostMapping("/delete")
    public Return delete(String conditionCode) {
        if (StringUtils.isEmpty(conditionCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        conditionService.delete(conditionCode);
        return new Return(Return.SUCCESS, "删除成功");
    }
}
